<?

//Data loading
$title='UNBALANCING REPORT';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new GridPDFReport("P","mm","Letter");
$fromDate = GetParam("fromDate","");
$toDate  = GetParam("toDate","");
$pdf->AddPage();
$Result = GridList ();
if ($Result) $pdf->GridTable($Result);
$pdf->Output();

function GridList () {
    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    list($day,$month,$year) = explode("/",GetParam("fromDate",""));
    $fromDate = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
    list($day,$month,$year) = explode("/",GetParam("toDate",""));
    $toDate   = date('Y-m-d',mktime(0,0,0,$month,$day,$year));



    $SQL = "SELECT nobkt, tglbkt,curr,sum(IF (dk = 'D', rupiah, 0) ) AS debet, sum(IF (dk = 'K', rupiah, 0) ) AS credit, sum(IF (dk = 'D', rupiah, 0) ) - sum(IF (dk = 'K', rupiah, 0) ) AS selisih FROM `ais_memorial` WHERE (tglbkt >= '$fromDate' AND tglbkt <= '$toDate') GROUP BY nobkt HAVING selisih <>0";
    $SQL .= " ORDER BY nobkt";

    //print $SQL."<br><br>\n\n\n\n";die();
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
    return $MyResult;
}

class GridPDFReport extends PDF {
    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        //$this->SetLeftMargin(5);
        $FDATE = explode('/',GetParam("fromDate",""));
        $TDATE = explode('/',GetParam("toDate",""));
        $DATEPRINT = date();

        $this->SetFont('arialn','',10);
        $this->Cell(200,5,$perush,0,1,'C',0);
        $this->Cell(200,5,$title,0,1,'C',0);
        $this->Cell(200,5,"AS OF : ".date('d/m/Y',mktime(0,0,0,$FDATE[1],$FDATE[0],$FDATE[2]))." - ".date('d/m/Y',mktime(0,0,0,$TDATE[1],$TDATE[0],$TDATE[2])),0,1,'C',0);
        //$this->Cell(200,5,"AS OF : ".date("F j, Y, g:i a"),0,1,'C',0);
        $this->SetLeftMargin(5);
        $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);
        $this->Line(5,30,210,30);
        $this->Ln();
        $this->Ln();
        //Header
        //Column widths
        $w=array(10,25,25,10,45,45,40);
        //$this->SetFont('arialn','',10);
        $this->Cell($w[0],7,"NO.",1,0,'C',0);
        $this->Cell($w[1],7,"NOBKT",1,0,'C',0);
        $this->Cell($w[2],7,"TGLBKT",1,0,'C',0);
        $this->Cell($w[3],7,"CURR",1,0,'C',0);
        $this->Cell($w[4],7,"DEBIT",1,0,'C',0);
        $this->Cell($w[5],7,"CREDIT",1,0,'C',0);
        $this->Cell($w[6],7,"SELISIH",1,0,'C',0);
        $this->Ln();
        $this->SetFont('arialn','',8);
    }

    function GridTable($Result) {
        //Column widths
        $w=array(10,25,25,10,45,45,40);
        //Data
        foreach($Result as $row)
        {
            $n++;
            $this->Cell($w[0],6,$n,'LR',0,'R');
            $this->Cell($w[1],7,$row["nobkt"],'LR',0,'L');
            $this->Cell($w[2],7,$row["tglbkt"],'LR',0,'L');
            $this->Cell($w[3],7,$row["curr"],'LR',0,'L');
            $this->Cell($w[4],7,FormatNumber($row["debet"]),'LR',0,'R');
            $this->Cell($w[5],7,FormatNumber($row["credit"]),'LR',0,'R');
            $this->Cell($w[6],7,FormatNumber($row["selisih"]),'LR',0,'R');
            $this->Ln();
        }

    }

    function Footer() {
        //Closure line
        $this->Cell(200,0,'','T');
        $this->Ln();
        $this->Cell(200,5,"  ( printed : ".date("F j, Y, g:i a")." )",0,1,'L',0);
    }

}


?>
